package swu.cx.flowgroup

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View

class circleProgress:View {
    //记录下载的进度
    var progress = 0f
        set(value) {
            field = value
            invalidate()
        }
    var progressBgColor = Color.CYAN
        set(value) {
            field = value
            circleBgPaint.color = value
        }
    var progressFgColor = Color.GREEN
        set(value) {
            field = value
            circleFgColor.color = value
        }
    var textColor = Color.BLACK
    private var cx = 0f
    private var cy = 0f
    private var radius = 0f
    private var mStrokeWidth = 60f
    private val circleBgPaint = Paint().apply {
        color = progressBgColor
        style = Paint.Style.STROKE
        strokeWidth = mStrokeWidth
    }
    private val circleFgColor = Paint().apply {
        color = progressFgColor
        style = Paint.Style.STROKE
        strokeWidth = mStrokeWidth
    }
    private val textPaint = Paint().apply {
        color = textColor
        style = Paint.Style.FILL
        textSize = 100f
        textAlign = Paint.Align.CENTER
    }
    constructor(context: Context):super(context){}
    constructor(context: Context,attr: AttributeSet):super(context,attr){
        //解析xml中配置的属性
        //将xml中传递过来的attrs解析出来
        //参数：1.attr：xml中为自定义的属性设置的值和属性名的集合
        //      2.R.styleable.circleProgress 依据哪个文件进行解析
        val typedArray:TypedArray = context.obtainStyledAttributes(attr,R.styleable.circleProgress)
        //取一个颜色
        //index：你要取自定义属性的哪个属性 styleable表示自定义的 类名_属性名
       progressBgColor = typedArray.getColor(R.styleable.circleProgress_backgroundColor,Color.BLACK)
        progressFgColor = typedArray.getColor(R.styleable.circleProgress_foregroundColor,Color.CYAN)
        textColor = typedArray.getColor(R.styleable.circleProgress_android_textColor,Color.BLACK)
        //回收资源
        typedArray.recycle()
    }
    constructor(context: Context,attr: AttributeSet,style:Int):super(context,attr,style){}

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        cx = width/2f
        cy = height/2f
        radius = if (width>height) height/2f-mStrokeWidth else width/2f-mStrokeWidth
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        Log.v("cx","child_Measure")
    }
    override fun onDraw(canvas: Canvas?) {
        Log.v("cx","onDraw")
        canvas?.drawCircle(cx,cy,radius,circleBgPaint)
        canvas?.drawArc(mStrokeWidth,mStrokeWidth,width.toFloat()-mStrokeWidth,height.toFloat()-mStrokeWidth,
                -90f,progress*360f,false,circleFgColor
            )
        val mtrics = textPaint.fontMetrics
        val space = (mtrics.descent-mtrics.ascent)/2f-mtrics.descent
        val text="${(progress*100).toInt()}%"
        canvas?.drawText(text,cx,cy+space,textPaint)
    }
}